﻿using System;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using Admintool.ExtensionMethods;
using Admintool.Infrastructure;
using EM.LVB.OMS.IPTool;

namespace Admintool
{
    partial class Program
    {
        [Command("gen sql for user reg by specified timeline")]
        public static void GenSQL4Reg(
            [Optional("", new string[] { "s" }, Description = "start timeline(YYYYMMDDHHMMSS) , use start of the today if not specfied ")]string start,
            [Optional("", new string[] { "e" }, Description = "end timeline(YYYYMMDDHHMMSS), use end of the today if not specfied ")]string end,
            [Optional(@"d:\GenSQL4UserReg.sql", new string[] { "o" }, Description = "output file")]string outputfile)
        {
            const string sql = "SELECT UID,DISPLAYNAME,SID,ORIGIN,IP,MOBPHONE,REGISTERTIME,EMUID,DEVICEINFO FROM DB_USER_INFO_{0}.T_LIVE_USER_INFO_{1} WHERE REGISTERTIME BETWEEN {2} AND {3} UNION ALL";
            StringBuilder sb = new StringBuilder();

            DateTime d1 = ConsoleHelper.GetValue<DateTime>(start, DateTime.Now.Date);
            DateTime d2 = ConsoleHelper.GetValue<DateTime>(end, DateTime.Now.Date.AddSeconds(86399));
            Enumerable.Range(0, 10).ForEach<int>((dbIndex, db) => {
                Enumerable.Range(0, 10).ForEach<int>((tableIndex, table) => {
                    sb.AppendLine(string.Format(sql, dbIndex, tableIndex, d1.ToUnixTimestamp(), d2.ToUnixTimestamp()));
                });
            });
            File.WriteAllText(outputfile, sb.ToString().TrimTail("UNION ALL"), Encoding.UTF8);
            ConsoleHelper.ShowMessage("====YOUR command has completed! start={0} ,end={1} ,path={2}", ConsoleColor.Green, d1, d2, outputfile);
        }


    }
}
